#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{

    ui->setupUi(this);
    this->resize(600,800);
    connButton = new QPushButton("connect",this);
    connButton->resize(120,40);
    connButton->move(300,600);
    hitButton = new QPushButton("don't hit me",this);
    hitButton->resize(120,40);
    //    QPushButton* sp = &hitButton;
    //    QPushButton* cp = &connButton;
    connFlag =false;
    connect(connButton, &QPushButton::clicked,
            this,&Widget::belicked);

}

void func()
{
    qDebug("test");
}
Widget::~Widget()
{
    delete ui;
}


void Widget::belicked()
{
    if(!connFlag)
    {
        connect(hitButton,&QPushButton::clicked,this,runAway);
        qDebug("connect");
        connButton->setText("disconnect");
    }
    else
    {
        disconnect(hitButton, &QPushButton::clicked,this,runAway);
        qDebug("disconnect");
        connButton->setText("connect");
    }
    connFlag=!connFlag;
}

void Widget::runAway()
{
    void (*p)() =func;
    p();
    hitButton->move(rand()%300,rand()%400);
}

